package com.mo.lib.utils;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.os.Build;

import androidx.annotation.RequiresApi;


import com.mo.lib.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @ author：mo
 * @ data：2021/7/29:16:50
 * @ 功能：快捷方式工具类
 */
public class ShortcutUtil {
    private Context mContext;
    private ShortcutManager shortcutManager;
    private String id, mShortLabel, mLongLabel;

    private Class<?> mClass;
    private int mIconId = R.drawable.aa;
    private Intent mIntent;
    List<ShortcutInfo> dynamicShortcuts = new ArrayList<>();

    public ShortcutUtil(Context mContext) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
            throw new UnsupportedOperationException("u can't instantiate me...");
        } else {
            this.mContext = mContext;
            shortcutManager = mContext.getSystemService(ShortcutManager.class);
        }
    }
    /** 获取快捷方式的id列表  tag：0==静态快捷方式的 、1==动态快捷方式的、2==国定快捷方式的   other==所有的*/
    @SuppressLint("NewApi")
    public void getShortcutInfoList(int tag){
        List<String> ids1 = new ArrayList<>();
        List<ShortcutInfo> infos=new ArrayList<>();
        if (tag==0){
            infos = shortcutManager.getManifestShortcuts();
        }else    if (tag==1){
            infos = shortcutManager.getDynamicShortcuts();
        }else    if (tag==2){
            infos = shortcutManager.getPinnedShortcuts();
        }else {
//            infos.addAll(shortcutManager.getManifestShortcuts());
            infos.addAll(shortcutManager.getDynamicShortcuts());
//            infos.addAll(shortcutManager.getPinnedShortcuts());
        }
        for (ShortcutInfo info : infos) {
            ids1.add(info.getId());
        }
        shortcutManager.removeDynamicShortcuts(ids1);
//        shortcutManager.removeLongLivedShortcuts(ids1);
//        shortcutManager.disableShortcuts(ids1);
    }
    /**
     * 动态删除
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public void delete() {
        /********* 移除弹出列表图标 **********/
        // 所有动态创建图标
        List<ShortcutInfo> infos1 = shortcutManager.getDynamicShortcuts();

        List<String> ids1 = new ArrayList<>();
        for (ShortcutInfo info : infos1) {
            ids1.add(info.getId());
        }

        // 禁用所有的快捷方式
        shortcutManager.disableShortcuts(ids1, "已禁用");
        shortcutManager.removeDynamicShortcuts(ids1);

        /********* 移除拖出来的桌面快捷图标 **********/
        // 放在桌面的图标
        List<ShortcutInfo> infos2 = shortcutManager.getPinnedShortcuts();

        List<String> ids2 = new ArrayList<>();
        for (ShortcutInfo info : infos2) {
            ids2.add(info.getId());
        }

        shortcutManager.disableShortcuts(ids2, "已禁用");
        shortcutManager.removeAllDynamicShortcuts();
    }

    public ShortcutUtil added() {
        if (mIntent == null) {
            mIntent = new Intent(mContext, mClass);
        }
        mIntent.setAction(Intent.ACTION_VIEW);
        mIntent.putExtra("shortcutId",id);
        @SuppressLint({"NewApi", "LocalSuppress"}) ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(mContext, id)
                .setShortLabel(mShortLabel)
                .setLongLabel(mLongLabel)
                .setIcon(Icon.createWithResource(mContext, mIconId))
//                    .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com")))
                .setIntent(mIntent)
                .build();
        dynamicShortcuts.add(shortcutInfo);
        mIntent=null;
        return this;
    }


    @SuppressLint("NewApi")
    public void createShortcut() {
        if (dynamicShortcuts.size()!=0){
            shortcutManager.setDynamicShortcuts(dynamicShortcuts);
        }
    }
    @SuppressLint("NewApi")
    public void createPinShortcut() {
        if (mIntent == null) {
            mIntent = new Intent(mContext, mClass);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (shortcutManager.isRequestPinShortcutSupported()) {//系统是否支持
                if (mIntent == null) {
                    mIntent = new Intent(mContext, mClass);
                }
                mIntent.setAction(Intent.ACTION_VIEW);//必须：不然跳不动
                ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(mContext, id)
                        .setShortLabel(mShortLabel)
                        .setLongLabel(mLongLabel)
                        .setIcon(Icon.createWithResource(mContext, mIconId))
//                    .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com")))
                        .setIntent(mIntent)
                        .build();
                //添加跳转意图
                Intent pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(shortcutInfo);
                PendingIntent successCallback = PendingIntent.getBroadcast(mContext, 0, pinnedShortcutCallbackIntent, 0);
                shortcutManager.requestPinShortcut(shortcutInfo, successCallback.getIntentSender());
            }
        }
    }

    public ShortcutUtil setId(String id) {
        this.id = id;
        return this;
    }

    public ShortcutUtil setmShortLabel(String mShortLabel) {
        this.mShortLabel = mShortLabel;
        return this;
    }


    public ShortcutUtil setmLongLabel(String mLongLabel) {
        this.mLongLabel = mLongLabel;
        return this;
    }

    public ShortcutUtil setmClass(Class<?> mClass) {
        this.mClass = mClass;
        return this;
    }

    public ShortcutUtil setmIconId(int mIconId) {
        this.mIconId = mIconId;
        return this;
    }

    public ShortcutUtil setmIntent(Intent mIntent) {
        this.mIntent = mIntent;
        return this;
    }


}
